import pygame
from const import *
from base_cls import Ball
from time import sleep
from utils import *


pygame.init()

screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('桌球模拟')

clock=pygame.time.Clock()

ball1=Ball(x=100,y=100,vx=900,vy=200,color=Color.gray1.value)
ball2=Ball(x=300,vx=1200,vy=800,color=Color.gray1.value)
ball3=Ball(x=100,y=200,vx=500,vy=1000,color=Color.gray1.value)
ball4=Ball(x=700,y=100,vx=-500,vy=100,color=Color.gray1.value)
ball5=Ball(x=400,y=100,vx=-500,vy=1000,color=Color.gray1.value)
ball6=Ball(x=500,y=100,vx=-500,vy=1000,color=Color.gray1.value)
ball7=Ball(x=600,y=100,vx=-500,vy=1000,color=Color.gray1.value)
ball8=Ball(x=100,y=150,vx=1200,vy=300,color=Color.gray1.value)
ball9=Ball(x=100,y=200,vx=1200,vy=300,color=Color.gray1.value)
ball10=Ball(x=100,y=300,vx=1200,vy=300,color=Color.gray1.value)
all_balls=[ball1,ball2,ball3,ball4,ball5,ball6,ball7,ball8,ball9,ball10]
# all_balls=[ball1]

all_balls=[]
maps=balls_map(15,180,185)
for k,v in maps.items():
    for i in v:
        all_balls.append(Ball(x=k,y=i,color=Color.red1.value))
all_balls.append(Ball(x=700,y=80,vx=-1700,vy=600,color=Color.gray1.value))

bgImage=pygame.image.load(ROOT_PATH.joinpath('images/table.jpg'))
bgImage=pygame.transform.scale(bgImage,(WIDTH,HEIGHT))
screen.blit(bgImage,(0,0))
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()

    screen.blit(bgImage,(0,0))
    # 实时绘制球体
    for ball in all_balls:
        ball.velocity(UNIT_TIME)
        ball.position(UNIT_TIME)
        check(ball, TABLE_LEFT, TABLE_RIGHT, TABLE_UP, TABLE_BOTTOM, all_balls)
        pygame.draw.circle(screen, ball.color, (round(ball.x), round(ball.y)), ball.radius)
    pygame.display.update()

    collide_balls=dict()
    for i,ball in enumerate(all_balls):
        ball.bump()
        collide_balls=ball.collide(all_balls,collide_balls)

    clock.tick(frames)  # 每一秒的帧数